<template>
  <div class="execution">
    <basic-container>
      <div class="sw-revenue-daily-report">
        <el-form :inline="true">
          <el-form-item label="日期">
            <el-date-picker
              v-model="search.date"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSearch">查询</el-button>
            <el-button type="primary" @click="saveOrUpdate"
              >保 存
            </el-button>
            <el-button type="primary" @click="refresh"
              >重新生成报表
            </el-button>
            <el-button type="primary" @click="handleExcelWrite"
              >导 出
            </el-button>
          </el-form-item>
        </el-form>
        <table class="el-table el-table--border" v-loading="loadingShow" element-loading-text="数据正在加载中...">
          <thead>
            <tr>
              <th colspan="2" class="bold">票款收入</th>
              <th colspan="2" class="bold">合计</th>
            </tr>
          </thead>
          <tr>
            <td rowspan="4" class="bold center">TVM<br />收入</td>
            <td class="center" width="250">钱箱票款</td>
            <td colspan="2">{{ reportData.cashBox.toFixed(2) }}</td>
          </tr>
          <tr>
            <td class="center">补币金额</td>
            <td colspan="2">{{ reportData.supplment.toFixed(2) }}</td>
          </tr>
          <tr>
            <td class="center">手工清出</td>
            <td colspan="2">{{ reportData.hand.toFixed(2) }}</td>
          </tr>
          <tr>
            <td class="bold center">小计</td>
            <td colspan="2">{{ reportData.subTvmTotal.toFixed(2) }}</td>
          </tr>
          <!-- <tr>
            <td class="center">钱箱差额</td>
            <td colspan="3">{{ reportData.boxDiff.toFixed(2) }}</td>
          </tr> -->
          <tr>
            <td rowspan="4" class="bold center">BOM<br />收入</td>
            <td class="center">营销车票</td>
            <td colspan="2">{{ reportData.marketTicket.toFixed(2) }}</td>
          </tr>
          <!-- <tr>
            <td class="center">日票</td>
            <td colspan="3">{{ reportData.dayTicket.toFixed(2) }}</td>
          </tr> -->
          <tr>
            <td class="center">预制单程票</td>
            <td colspan="2">{{ reportData.yzdcp.toFixed(2) }}</td>
          </tr>
          <tr>
            <td class="center">应急纸票</td>
            <td colspan="2">{{ reportData.yjzp.toFixed(2) }}</td>
          </tr>	 
          <tr>
            <td class="bold center">BOM实收总金额小计</td>
            <td colspan="2">{{ reportData.subBomTotal.toFixed(2) }}</td>
          </tr>
          <tr>
            <td colspan="2" class="bold center">补售票员短款</td>
            <td colspan="2">{{ reportData.conductorShort.toFixed(2) }}</td>
          </tr>
          <!--  <tr>
            <td colspan="2" class="bold center">加收票款</td>
            <td colspan="2">{{ reportData.addMoney.toFixed(2) }}</td>
          </tr> -->
          <tr>
            <td colspan="2" class="bold center">交接长款</td>
            <td colspan="2">{{ reportData.conductorLong.toFixed(2) }}</td>
          </tr>
          <tr>
            <td colspan="2" class="bold center">其它票款金额</td>
            <td colspan="2">{{ reportData.other.toFixed(2) }}</td>
          </tr>
          <tr>
            <td colspan="2" class="bold center">营收总金额</td>
            <td colspan="2">{{ reportData.total.toFixed(2) }}</td>
          </tr>
          <!-- <tr>
            <td colspan="2" class="center" height="150">备注</td>
            <td colspan="3">{{ reportData.remark }}</td>
          </tr> -->

          <!-- <tr>
            <td colspan="2" class="center">客运值班员</td>
            <td class="center">{{ reportData.passengerAttendName }}</td>
            <td class="center">客运值班员员工号</td>
            <td class="center">{{ reportData.passengerAttendJobNo }}</td>
          </tr>
          <tr>
            <td colspan="2" class="center">复核人</td>
            <td class="center" width="350">{{ reportData.attendName }}</td>
            <td class="center">复核人员工号</td>
            <td class="center" width="350">{{ reportData.attendJobNo }}</td>
          </tr> -->
          <tr>
            <td colspan="1" class="center">录入人</td>
            <td class="center">
              <el-select v-model="reportData.passengerAttendName" placeholder="" style="width: 190px" @change="changeAttendName" clearable filterable>
                <el-option
                  v-for="(item,index) in attendNames"
                  :key="index"
                  :label="item.nikename"
                  :value="item.nikename">
                </el-option>
              </el-select>
            </td>
            <td class="center">录入人员工号</td>
            <td class="center">
              <el-select v-model="reportData.passengerAttendJobNo" placeholder="" style="width: 190px" clearable filterable>
                <el-option
                  v-for="(item,index) in attendJobNos"
                  :key="index"
                  :label="item.userCode"
                  :value="item.userCode">
                </el-option>
              </el-select>
            </td>
          </tr>
          <!-- <tr>
            <td colspan="2" class="center">复核人</td>
            <td class="center" width="350">{{ reportData.attendName }}</td>
            <td class="center">复核人员工号</td>
            <td class="center" width="350">{{ reportData.attendJobNo }}</td>
          </tr> -->
          <tr>
            <td colspan="1" class="center">复核人</td>
            <td class="center" width="230">
              <el-select v-model="reportData.attendName" placeholder="" style="width: 190px" @change="changeReviewer" clearable filterable>
                <el-option
                  v-for="(item,index) in attendNames"
                  :key="index"
                  :label="item.nikename"
                  :value="item.nikename">
                </el-option>
              </el-select>
            </td>
            <td class="center">复核人员工号</td>
            <td class="center" width="230">
              <el-select v-model="reportData.attendJobNo" placeholder="" style="width: 190px" clearable filterable>
                <el-option
                  v-for="(item,index) in attendJobNos"
                  :key="index"
                  :label="item.userCode"
                  :value="item.userCode">
                </el-option>
              </el-select>
            </td>
          </tr>
        </table>
      </div>
    </basic-container>
  </div>
</template>

<script>
import store from "@/store";
import { dateFormat } from "@/util/date";
import request from "@/router/axios";
import { handleExcelResponse } from "@/util/util";
import { getSysUserToRevenue } from "@/api/admin/user";

export function exportByPageContent(query) {
  return request({
    url: "/stm/swstreportquery/exportByPageContent",
    method: "post",
    data: query,
    responseType: "blob",
    headers: {
      "Content-Type": "application/json;charset=UTF-8"
    }
  });
}
export function refreshStationincome(query) {
  return request({
    url: "/stm/swstreportquery/refreshStationincome",
    method: "post",
    //data: query
    params: query
  });
}
export function saveOrUpdate(query) {
  return request({
    url: "/stm/swstreportquery/saveOrUpdateStationincome",
    method: "post",
    data: query
  });
}
export default {
  name: "swrevenuedailyreport",
  data() {
    return {
      search: {
        date: ""
      },
      reportData: {
        cashBox: 0.00, //钱箱票款
        supplment: 0.00, //补币金额
        hand: 0.00, //手工清出
        subTvmTotal: 0.00,
        boxDiff: 0, //钱箱差额
        marketTicket: 0.00, //营销车票
        dayTicket: 0.00, //日票
        yzdcp: 0.00, //预制单程票
        yjzp: 0.00, //应急纸票	
        subBomTotal: 0.00, //BOM实收总金额小计
        conductorShort: 0.00, //补售票员短款
        addMoney: 0.00, //加收票款
        conductorLong: 0.00, //客值交接长款
        other: 0.00, //其他
        total: 0.00, //营收总金额
        remark: "", //备注
        passengerAttendName: "", //客运值班员
        passengerAttendJobNo: "", //客运值班员工号
        attendName: "", // 复核人
        attendJobNo: "" //复核人工号
      },
      attendNames:[],
      attendJobNos:[],
      loadingShow: false,
      newData: {}
    };
  },
  created() {
    const date = new Date();
    if (date.getHours() < 6) {
      date.setDate((date.getDate() - 1));
    }
    //date.setTime(date.getTime() - 3600 * 1000 * 24);
    this.search.date = dateFormat(date, "yyyy-MM-dd");
    this.onSearch();
    this.getPassenger();
    this.newData = JSON.parse(JSON.stringify(this.reportData));
  },
  computed: {},
  methods: {
    refresh(){
      this.$confirm("确认重新生成报表?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.loadingShow = true;
        let p = {
          statisticsDate: this.search.date
        };
        refreshStationincome(p).then((response)=>{
          this.$message.success("重新生成报表成功");
          this.reportData = response.data.data;
          this.onSearch();
          this.loadingShow = false;
        })
        .catch(() => {
          this.$message.error("重新生成报表失败！");
          this.loadingShow = false;
        });
      });
    },
    saveOrUpdate(){
      this.$confirm("确认要修改吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        saveOrUpdate(this.reportData)
          .then(data => {
            this.$message.success("保存成功");
            this.onSearch();
          })
          .catch(() => {
            this.$message.error("保存失败！");
          });
      });
    },
    getPassenger(){
      getSysUserToRevenue().then((response)=>{
        this.attendNames = response.data.data;
        this.attendJobNos = response.data.data;
      });
    },
    changeAttendName(data){
        var objVal={};
        this.attendJobNos.forEach((val)=>{
          if(val.nikename==data){
            objVal=val;
          }
        })
        this.reportData.passengerAttendJobNo=objVal.userCode;
    },
    changeReviewer(data){
        var objVal={};
        this.attendJobNos.forEach((val)=>{
          if(val.nikename==data){
            objVal=val;
          }
        })
        this.reportData.attendJobNo=objVal.userCode;
    },
    onSearch() {
      this.loadingShow = true;
      request({
        url: "/stm/swstreportquery/stationincomerealtime/" + this.search.date,
        method: "get"
      }).then(res => {
        if (res.data.data) {
          this.reportData = res.data.data;
        }else{
          this.reportData = this.newData;
        }
        this.loadingShow = false;
      }).catch(error=> {
          this.loadingShow = false;
      })
    },
    async handleExcelWrite() {
      // 导出
      let reqdata = this.reportData;
      let p = JSON.stringify(reqdata);
      let res = await exportByPageContent(p);
      handleExcelResponse(res, "车站营收日报表.xlsx");
    }
  }
};
</script>
<style lang="scss">
.sw-revenue-daily-report {
  table {
    border: 1px solid #b7babf;
    th {
      text-align: center;
      border: 1px solid #b7babf;
    }
    td {
      padding: 12px 10px;
      border: 1px solid #b7babf;
    }
    &.el-table--border::after,
    &.el-table::before {
      background-color: #b7babf;
    }
    width: 70%
  }
  .center {
    text-align: center;
  }
  .bold {
    font-weight: bold;
  }
}
</style>
